﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication6
{
    class Dynamicfibonaci
    {

        private int i_n;
        private long l_fibonaci;
        private int i_elapsetime;


        /// <summary>
        /// Constructor of DynamicFibonaci Class
        /// </summary>
        /// <param name="n"></param>
        public Dynamicfibonaci(int n)
        {
            this.i_n = n;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public long getFibonaci()
        {
            return l_fibonaci;
        }

        /// <summary>
        /// Get Elapstime
        /// </summary>
        /// <returns></returns>
        public int getElapsetimes()
        {
            return i_elapsetime;
        }



        /// <summary>
        /// Compute the Fib[n]
        /// </summary>
        /// <param name="n"></param>
        /// <returns>Fib[n]</returns>
        public long Fib(int n)
        {
            long l_fib_0 = 0; //Fib[0] = 0
            long l_fib_1 = 1; //Fib[2] =1
            long l_fib_n = 0; //Fib[n] = Fib[1];
            int i;
            if (n == 0)
            {
                return l_fib_0;
            }
            if (n == 1)
            {
                return l_fib_1;
            }
            for (i = 2; i <= n; i++)
            {
                l_fib_n = l_fib_0 + l_fib_1;
                l_fib_0 = l_fib_1;
                l_fib_1 = l_fib_n;
            }
            return l_fib_n;
        }


        /// <summary>
        /// Count the time elapse
        /// </summary>
        public void Implement()
        {
            DateTime dt_start = new DateTime();
            DateTime dt_end = new DateTime();
            dt_start = DateTime.Now;
            this.l_fibonaci = Fib(i_n);
            dt_end = DateTime.Now;
            TimeSpan ts_timespans = dt_end - dt_start;
            this.i_elapsetime = ts_timespans.Seconds * 1000 + ts_timespans.Milliseconds;
        }
    }
}
